package com.tsign.demo.util;

import com.github.lianjiatech.retrofit.spring.boot.core.ErrorDecoder;
import com.github.lianjiatech.retrofit.spring.boot.exception.ReadResponseBodyException;
import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class BaseErrorDecoder implements ErrorDecoder {

    @Override
    public RuntimeException invalidRespDecode(Request request, Response response) {

        if (!response.isSuccessful()) {
            String responseBody = null;
            try {
                responseBody = RetrofitUtils.readResponseBody(response);
                if (responseBody != null) {
                    return null;
                }
            } catch (ReadResponseBodyException e) {
                // do nothing
            }

        }

        return null;
    }

    @Override
    public RuntimeException ioExceptionDecode(Request request, IOException cause) {
        throw new RuntimeException("error");
    }
}
